/*
 * Copyright 2023 AntGroup CO., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

package com.antgroup.geaflow.collection.array;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class PrimitiveArrayFactory {

    private static final Function<Integer, PrimitiveArray> DEFAULT_BUILDER = ObjectArray::new;
    private static final Map<Class<?>, Function<Integer, PrimitiveArray>> UNMARSHALL = new HashMap<>();

    static {
        UNMARSHALL.put(Integer.class, IntArray::new);
        UNMARSHALL.put(Integer.TYPE, IntArray::new);
        UNMARSHALL.put(Long.class, LongArray::new);
        UNMARSHALL.put(Long.TYPE, LongArray::new);
        UNMARSHALL.put(Double.class, DoubleArray::new);
        UNMARSHALL.put(Double.TYPE, DoubleArray::new);
        UNMARSHALL.put(Byte.class, ByteArray::new);
        UNMARSHALL.put(Byte.TYPE, ByteArray::new);
        UNMARSHALL.put(Float.class, FloatArray::new);
        UNMARSHALL.put(Float.TYPE, FloatArray::new);
        UNMARSHALL.put(Boolean.class, BooleanArray::new);
        UNMARSHALL.put(Boolean.TYPE, BooleanArray::new);
        UNMARSHALL.put(Short.class, ShortArray::new);
        UNMARSHALL.put(Short.TYPE, ShortArray::new);
        UNMARSHALL.put(byte[].class, BytesArray::new);
    }

    public static PrimitiveArray getCustomArray(Class<?> type, int capacity) {
        return UNMARSHALL.getOrDefault(type, DEFAULT_BUILDER).apply(capacity);
    }
}
